/*
 *
 * Copyright © 2007 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 */

import java.util.Vector;

import javax.wireless.messaging.*;


/**
 * Sun Microsystems' WMADemo MMSMessage class.
 *
 */
public class MMSMessage {
    private String destination;
    private final Vector parts = new Vector();
    private String subject;

    /**
     * Check the phone number for validity
     * Valid phone numbers contain only the digits 0 thru 9, and may contain
     * a leading '+'.
     */
    private static boolean isValidPhoneNumber(final String address) {
        final String protocol = "mms://";

        if (!address.startsWith(protocol))
		    return false;

        final String number = address.substring(protocol.length());
        final char[] chars = number.toCharArray();

        if (chars.length == 0)
			return false;

        int startPos = 0;

        // initial '+' is OK
        if (chars[0] == '+')
			startPos = 1;

        for (int i = startPos; i < chars.length; ++i)
			if (!Character.isDigit(chars[i])){
				System.out.println("hhhhhhhhhh");
				return false;
			}
        return true;
    }

    public String getSubject() {
        return this.subject;
    }

    public void setSubject(final String subject) {
        this.subject = subject;
    }

    public String getDestination() {
        return this.destination;
    }

    public void setDestination(final String destination) {
        if (!MMSMessage.isValidPhoneNumber(destination))
			throw new IllegalArgumentException("Invalid phone number ("+destination+").");

        this.destination = destination;
    }

    public MessagePart[] getParts() {
        final MessagePart[] partsArray = new MessagePart[this.parts.size()];
        this.parts.copyInto(partsArray);

        return partsArray;
    }

    public void addPart(final MessagePart part) {
        this.parts.addElement(part);
    }
}
